/*
		ServEngine framework library
		A library to fast develop JEE online applications
	
		Copyright 2008 José Ignacio de Córdoba Álvaro

		Licensed under the Apache License, Version 2.0 (the "License");
		you may not use this file except in compliance with the License.
		You may obtain a copy of the License at

				http://www.apache.org/licenses/LICENSE-2.0

		Unless required by applicable law or agreed to in writing, software
		distributed under the License is distributed on an "AS IS" BASIS,
		WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
		See the License for the specific language governing permissions and
		limitations under the License.
	
		Jose Ignacio de Cordoba Alvaro
		http://ignacio.decordoba.com
*/

package com.servengine.tag;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

@Entity
public class Tag implements java.io.Serializable,Comparable<Tag>
{
	String body,locale;
	int level;
//	Set<TaggedItem> taggedItems;
	Tag relatedTag;
	Set<Tag> relatedTags;
	public Tag() {}
	public Tag(String body, String locale)
	{
//		taggedItems = new HashSet<TaggedItem>();
	  relatedTags = new HashSet<Tag>();
		this.body=body;
		this.locale=locale;
//		this.taggedItems=taggedItems;
//	  this.relatedTags=relatedTags;
//		float averageItemsPerTag = totalItemCount / totalTagCount;
//		level = (int)((itemCount / averageItemsPerTag) + 1);
	}

	public int compareTo(Tag tag)
	{
/*		int result = getItemCount()-tag.getItemCount();
		if (result==0)
			result=getRelatedTagCount()-tag.getRelatedTagCount();
		if (result==0)*/
			return body.compareTo(tag.getBody());
		///return result;
	}

	public boolean equals (Object item)
	{
		if (body==null)
			return ((Tag)item).getBody()==null;
		return ((Tag)item).getBody().equals(body);
	}

	public int hashCode()
	{
		return body.hashCode();
	}
	
	@Id
	public String getBody()
	{
		return body;
	}

	public String getLocale()
	{
		return locale;
	}

/*	@Transient
	public int getItemCount()
	{
		return getTaggedItems().size();
	}

	@Transient
	public int getRelatedTagCount()
	{
		return getRelatedTags().size();
	}*/
	
	public String toString()
	{
		return body;
	}

	public void setLevel(int level)
	{
		this.level = level;
	}

	public int getLevel()
	{
		return level;
	}

/*	@OneToMany(mappedBy="tag",fetch=FetchType.LAZY)
	public void setTaggedItems(Set<TaggedItem> taggedItems)
	{
		this.taggedItems = taggedItems;
	}

	@OneToMany(mappedBy="tag",fetch=FetchType.LAZY)
	public Set<TaggedItem> getTaggedItems()
	{
		return taggedItems;
	}*/

	public void setRelatedTags(Set<Tag> relatedTags)
	{
		this.relatedTags = relatedTags;
	}

	@OneToMany(mappedBy="relatedTag",fetch=FetchType.LAZY)
	public Set<Tag> getRelatedTags()
	{
		return relatedTags;
	}

	@Id
	public void setBody(String body)
	{
		this.body = body;
	}

	public void setLocale(String locale)
	{
		this.locale = locale;
	}

	public void setRelatedTag(Tag relatedTag)
	{
		this.relatedTag = relatedTag;
	}

	@ManyToOne(fetch=FetchType.LAZY)
	public Tag getRelatedTag()
	{
		return relatedTag;
	}
}
